package com.pansoft.openplanet.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.efounder.chat.activity.BaseActivity;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.constant.IPAssetsConstant;
import com.pansoft.openplanet.util.EosNetParamUtil;

/**
 * @author will
 * 选择钱包类型
 */
public class WalletTypeSelectActivity extends BaseActivity implements View.OnClickListener {
    //创建钱包
    public static final int MODE_CREATE_NEW = 1;
    //恢复钱包
    public static final int MODE_RESTORE_OLD = 2;

    private int mode = MODE_CREATE_NEW;
    /** 是否是创建卡片冷钱包 **/
    private boolean isCardColdWallet;

    /**
     * @param context
     * @param mode    创建或者恢复钱包
     */
    public static void start(Context context, int mode) {
        Intent starter = new Intent(context, WalletTypeSelectActivity.class);
        starter.putExtra("mode", mode);
        context.startActivity(starter);
    }

    /**
     * @param context
     * @param mode    创建或者恢复钱包
     */
    public static void start(Context context, int mode, boolean isCardColdWallet) {
        Intent starter = new Intent(context, WalletTypeSelectActivity.class);
        starter.putExtra("mode", mode);
        starter.putExtra("isCardColdWallet", isCardColdWallet);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wallet_type);
        if (getIntent().getExtras() != null) {
            isCardColdWallet = getIntent().getBooleanExtra("isCardColdWallet", false);
            if (getIntent().getIntExtra("mode", MODE_CREATE_NEW) == MODE_CREATE_NEW) {
                mode = MODE_CREATE_NEW;
            } else {
                mode = MODE_RESTORE_OLD;
            }
        }
        initView();

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_type_eth) {
            WalletAvatarAndNameSetupActivity.start(WalletTypeSelectActivity.this, mode,
                    IPAssetsConstant.CHAIN_ID_ETH, isCardColdWallet);
            finish();
        } else if (id == R.id.ll_type_btc) {
            WalletAvatarAndNameSetupActivity.start(WalletTypeSelectActivity.this, mode,
                    IPAssetsConstant.CHAIN_ID_BTC, isCardColdWallet);
            finish();
        } else if (id == R.id.ll_type_eos) {
//            showAlert("EOS钱包");
//            AccountDao accountDao = new AccountDao(this);
//            List<Account> accountList = accountDao.getAllAccount(EosNetParamUtil.getEosChainEnum());
//            boolean hasUnActivated = false;
//            String unActivatedPubKey = "";
//            if (accountList.size() > 0) {
//                for (int i = 0; i < accountList.size(); i++) {
//                    Account account = accountList.get(i);
//                    if (!account.isActivate()) {
//                        hasUnActivated = true;
//                        unActivatedPubKey = account.getEosActivePubKey();
//                    }
//                }
//            }
//            if (!hasUnActivated || mode != MODE_CREATE_NEW) {
                WalletAvatarAndNameSetupActivity.start(WalletTypeSelectActivity.this, mode,
                        EosNetParamUtil.getEosChainId());
//            } else {
//                Account account = accountDao.getEosAccountByActivePublicKey(unActivatedPubKey);
//                Intent intent = new Intent(this, CreateEosWalletActivity.class);
//                intent.putExtra("active_pub_key", account.getEosActivePubKey());
//                intent.putExtra("wallet_name", account.getAcccountName());
//                intent.putExtra("wallet_avatar", account.getAccountAvatar());
//                startActivity(intent);
//            }
            finish();
        }
    }

    private void initView() {
        LinearLayout llTypeEth = (LinearLayout) findViewById(R.id.ll_type_eth);
        LinearLayout llTypeBtc = (LinearLayout) findViewById(R.id.ll_type_btc);
        LinearLayout llTypeEos = (LinearLayout) findViewById(R.id.ll_type_eos);
        llTypeBtc.setOnClickListener(this);
        llTypeEos.setOnClickListener(this);
        llTypeEth.setOnClickListener(this);
        if (isCardColdWallet) {
            llTypeEos.setVisibility(View.INVISIBLE);
        }
    }

    private void showAlert(String name) {
        new AlertDialog.Builder(this).setTitle(name)
                .setMessage(name + ResStringUtil.getString(R.string.open_planet_asset_wallet_type_import_not_support))
                .setCancelable(true)
                .setPositiveButton(ResStringUtil.getString(R.string.common_text_confirm), null).show();
    }
}
